import { ApiCall } from "tsrpc";
import { ReqSubmintExp, ResSubmintExp } from "../../shared/protocols/Experiment/PtlSubmintExp";
import { Global } from '../../Global/index';

export default async function (call: ApiCall<ReqSubmintExp, ResSubmintExp>) {
    // TODO
    const user = call.currentUser
    const { student,experiment,submitTime,score,answers } = call.req
    
    if(user.role === 'student'){
        await Global.collection('userExperiments').insertOne({
            student:user._id,
            experiment,
            submitTime,
            score,
            answers
        })
    }
    else{
        await Global.collection('userExperiments').updateOne({
            student,
            experiment,   
        },{
            $set:{
                score,
                answers
            }
        })
    }

    call.succ({

    })
}